<style>
    .order-header-first {
        font-size: small;color: grey;
    }
    .order-header {
        font-size: small;color: grey;padding-left: 2%
    }
    .order-header-text {
        padding-left: 5px;
    }
</style>

@foreach($paginator as $row)

    <div id="supplier_{{ $row->delivery_no }}" class="box box-primary">
        <div class="box-header with-border" style="background-color: #f1f9fd">
            <span class="order-header-first">申请单号</span><span class="order-header-text">{{ $row->delivery_no }}</span>
            <span class="order-header">订单号</span><a target="_blank" href="/xqc/customer-delivery/customer-order-detail?no8={{ $row->order_no ?? '' }}"><span class="order-header-text">{{ $row->order_no }}</span> <i class="fa fa-search" style="font-size: small;"></i></a>
            <span class="order-header">下单时间</span><span class="order-header-text">{{ $row->order_at ?? '' }}</span>
            <span class="order-header">发货站点</span><span class="order-header-text">{{ $row->area->area ?? '' }}</span>
            <span class="order-header">平台</span><span class="order-header-text">{{ $row->plat->title ?? '' }}</span>
            <span class="order-header">申请员</span><span class="order-header-text">{{ \App\Util\User::getUsernameById($row->seller_id) ?? '' }}</span>
            <span class="order-header">订单状态</span><span class="order-header-text">{{ \App\Models\CustomerDelivery\CustomerDelivery::$_status[$row->status] ?? '' }}</span>
            <div class="box-tools pull-right">
                @if($row->status == 3 && \Encore\Admin\Facades\Admin::user()->can('customer.delivery-fba.back'))
                    <a href="javascript:void(0);" class="btn btn-box-tool grid-row-apply" data-id="{{ $row->id }}">重新申请订单</a>
                @endif
            </div>
        </div>
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th style="width: 2%"></th>
                    <th>实际发货订单号</th>
                    <th>SKU</th>
                    <th>亚马逊平台SKU</th>
                    <th style="width: 15%;">产品名称</th>
                    <th>申发数量</th>
                    <th>已发数量</th>
                    <th>可发账号</th>
                    <th>可发库存</th>
                    <th>标记状态</th>
                    <th>申请备注</th>
                    <th>发货状态</th>
                    <th>发货时间</th>
                    <th>发货备注</th>
                    <th>上传平台状态</th>
                    <th>上传平台时间</th>
                    <th>物流追踪号</th>
                    <th>物流运输方式</th>
                    <th>同步物流</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($row->detail as $item)
                    @php
                        $areaStockNum = \App\Models\SalesReport\FBAInventory::getACccountFbaInventory($item->account_id, $row->area_id, $item->psku_id);
                        $amazonAccountOptions = editableSourceJsonOption(\App\Models\SalesReport\FBAInventory::getACccountOptionsForFBA($row->area_id, $item->psku_id));
                    @endphp
                    <tr>
                        <td style="width: 2%">
                            @if($areaStockNum >= $item->apply_num)
                                @if($item->status == 3)
                                    <input type="checkbox" class="grid-row-checkbox" data-id="{{ $item->id }}" />
                                @endif
                            @endif
                        </td>
                        <td>
                            @if(\Encore\Admin\Facades\Admin::user()->can('customer.delivery-fba.edit') && $item->status != 5)
                                <span style="font-size: 85%;padding: .2em 0 .3em;">
                                <a class="grid-editable-delivery_order_no" href="#" data-type="text"
                                   data-pk="{{ $item->id }}"
                                   data-url="{{ '/xqc/customer-delivery/delivery-fba/'.$item->id }}"
                                   data-value="{{ $item->delivery_order_no }}">
                                    {{ $item->delivery_order_no }}
                                </a>
                            </span>
                            @else
                                {{ $item->delivery_order_no }}
                            @endif
                        </td>
                        <td>{{ $item->sku->psku_code ?? '' }}
                            @if($row->status == 5 && \Encore\Admin\Facades\Admin::user()->can('customer.delivery-fba.sync'))
                                @if($item->sync_status == 0)
                                    <button class="btn btn-xs btn-success list-modify-modal" data-id="{{ $item->id }}"
                                        data-action="modify">编辑物流</button>
                                @endif
                            @endif
                        </td>
                        <td>{{ $item->seller_sku ?? '' }}</td>
                        <td style="width: 15%;">{{ $item->sku->psku_cn ?? '' }}</td>
                        <td>{{ $item->apply_num }}</td>
                        <td>{{ $item->ship_num }}</td>
                        <td>
                            @if(\Encore\Admin\Facades\Admin::user()->can('customer.delivery-fba.edit') && $item->status == 3)
                                <a id="grid-editable-account_id-{{ $item->id }}" href="#"
                                   data-type="select"
                                   data-url="{{ '/xqc/customer-delivery/delivery-fba/'.$item->id }}"
                                   data-value="{{ $item->account_id.'_'.$item->seller_sku }}"
                                   data-pk="{{ $item->id }}">
                                    {{ \App\Models\Basics\SysPlatAccount::getAccountById($item->account_id) ?? '' }}
                                </a>

                                <script language="JavaScript" type="text/javascript">
                                    $(function () {
                                        let id = "grid-editable-account_id-{{ $item->id }}";
                                        $('#'+id).editable({
                                            "emptytext":"<i class=\"fa fa-pencil\"><\/i>",
                                            "showbuttons":false,
                                            "mode":"inline",
                                            "name":"account_id",
                                            "source":'{!! $amazonAccountOptions !!}',
                                            success: function(response, newValue) {
                                                $.pjax.reload("#pjax-container");
                                                if (typeof response === 'object') {
                                                    if (response.status) {
                                                        toastr.success(response.message);
                                                    } else {
                                                        swal(response.message, '', 'error');
                                                    }
                                                }
                                            }
                                        });
                                    });
                                </script>
                            @else
                                {{ \App\Models\Basics\SysPlatAccount::getAccountById($item->account_id) ?? '' }}
                            @endif
                        </td>

                        <td>{{ $areaStockNum }}</td>

                        <td>
                            @if($item->error_status == 1)
                                <span style="color: red">
                                    {{ \App\Models\CustomerDelivery\CustomerDeliveryDetail::$_errorStatus[$item->error_status] }}
                                </span>
                            @else
                                {{ \App\Models\CustomerDelivery\CustomerDeliveryDetail::$_errorStatus[$item->error_status] }}
                            @endif
                        </td>
                        <td>{{ $row->remarks }}</td>
                        <td>
                            @if($item->status == 3)
                                <span style="color: #00a65a">
                                    {{ \App\Models\CustomerDelivery\CustomerDeliveryDetail::$_status[$item->status] }}
                                </span>
                            @else
                                {{ \App\Models\CustomerDelivery\CustomerDeliveryDetail::$_status[$item->status] }}
                            @endif
                        </td>
                        <td>{{ $item->delivered_at ?? '' }}</td>
                        <td>
                            @if(\Encore\Admin\Facades\Admin::user()->can('customer.delivery-fba.edit'))
                            <span style="font-size: 85%;padding: .2em 0 .3em;">
                                <a class="grid-editable-remarks" href="#" data-type="text"
                                   data-pk="{{ $item->id }}"
                                   data-url="{{ '/xqc/customer-delivery/delivery-fba/'.$item->id }}"
                                   data-value="{{ $item->delivery_remarks }}">
                                    {{ $item->delivery_remarks }}
                                </a>
                            </span>
                            @else
                                {{ $item->delivery_remarks }}
                            @endif
                        </td>

                        <td>{{ \App\Models\CustomerDelivery\CustomerDelivery::$_platSyncStatus[$row->plat_sync_status] }}</td>
                        <td>{{ $row->plat_sync_at ?? '' }}</td>
                        <td>{{ $item->tracking_no ?? '' }}</td>
                        <td>{{ $item->logistics_type ?? '' }}</td>
                        <td>
                            @if($row->status == 5)
                                @if($item->sync_status == 0)
                                <button class="btn btn-xs btn-twitter sync"
                                        data-id="{{ $row->id }}"
                                        data-account_id="{{ $item->account_id }}"
                                        data-delivery_order_no="{{ $item->delivery_order_no ?? '' }}">
                                    <i class="fa fa-retweet"></i>
                                    <span id="loading{{ $row->id }}">同步</span>
                                </button>
                                @else
                                    {{ \App\Models\CustomerDelivery\CustomerDeliveryDetail::$_syncStatus[$item->sync_status] }}
                                @endif
                            @else
                                {{ \App\Models\CustomerDelivery\CustomerDeliveryDetail::$_syncStatus[$item->sync_status] }}
                            @endif
                        </td>
                        <td>
                            @if($item->status == 3 && \Encore\Admin\Facades\Admin::user()->can('customer.delivery-fba.back'))
                                <a href="javascript:void(0);" data-id="{{ $item->id }}" class="btn btn-xs btn-default grid-item-apply">重新申请</a>
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>

        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>国家</th>
                    <th>收货人</th>
                    <th width="15%">收货地址</th>
                    <th>州</th>
                    <th>城市</th>
                    <th>邮编</th>
                    <th>电话</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>{{ $row->receiver->country ?? '' }}</td>
                    <td>{{ $row->receiver->receiver ?? '' }}</td>
                    <td>{{ $row->receiver->address ?? '' }}</td>
                    <td>{{ $row->receiver->state ?? '' }}</td>
                    <td>{{ $row->receiver->city ?? '' }}</td>
                    <td>{{ $row->receiver->postcode ?? '' }}</td>
                    <td>{{ $row->receiver->phone ?? '' }}</td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
@endforeach

<div class="modal fade" id="tModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- 内容会加载到这里 -->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="modifyModal" tabindex="-1" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- 内容会加载到这里 -->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script language="JavaScript" type="text/javascript">
    $(function () {


        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
            if (this.checked) {
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $(this).closest('tr').css('background-color', '');
            }
        });

        let _ajaxConfirm = function(action,param,resolve){
            $.ajax({
                method: 'post',
                url: '/xqc/customer-delivery/delivery-fba-ajax',
                data: {
                    _token: "{{ csrf_token() }}",
                    action: action,
                    param: param
                },
                success: function (data) {
                    resolve(data);
                }
            });
        };

        //更新订单号 (区分订单批次)
        $('.grid-editable-delivery_order_no').editable({
            "emptytext":"<i class=\"fa fa-pencil\"><\/i>",
            //"showbuttons":false,
            "mode":"inline",
            "name":"delivery_order_no",
        });

        //更新备注
        $('.grid-editable-remarks').editable({
            "emptytext":"<i class=\"fa fa-pencil\"><\/i>",
            //"showbuttons":false,
            "mode":"inline",
            "name":"delivery_remarks",
        });

        //重新申请此单
        $('.grid-row-apply').on('click', function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            swal({
                title: "确定重新申请？",
                text : "确定要重新申请此单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('backReapply',id,resolve)
                    });
                }
            }).then(function(result) {
                let data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success(data.message);
                        $.pjax.reload('#pjax-container');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        //重新申请此商品
        $('.grid-item-apply').on('click', function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            swal({
                title: "确定重新申请？",
                text : "确定要重新申请此商品吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('backReapplyItem',id,resolve)
                    });
                }
            }).then(function(result) {
                let data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success(data.message);
                        $.pjax.reload('#pjax-container');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        //撤销商品ERROR标记
        $('.grid-revoke-error').unbind('click').click(function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            $.ajax({
                method: 'post',
                url: '/xqc/customer-delivery/delivery-fba-ajax',
                data: {
                    _token: "{{ csrf_token() }}",
                    action: 'revokeErrorItem',
                     param: id
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            toastr.success(data.message);
                            $.pjax.reload('#pjax-container');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        });

        //标记商品ERROR
        $('.grid-mark-error').unbind('click').click(function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            swal({
                title: "确定标记？",
                text : "确定要标记此商品ERROR吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('markErrorItem',id,resolve)
                    });
                }
            }).then(function(result) {
                let data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        $.pjax.reload('#pjax-container');
                        toastr.success(data.message);
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        //编辑物流
        $('.list-modify-modal').on('click', function(e) {
            e.preventDefault();
            let id = $(this).data('id');
            let action = $(this).data('action');
            $("#modifyModal").modal({
                remote : '/xqc/customer-delivery/delivery-fba-modal?param=' + id + '&action=' + action,
            });
        });
        $("#modifyModal").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
            $(this).find(".modal-content").children().remove();
        });

        //同步物流
        $('.sync').unbind('click').click(function(e) {
            e.preventDefault();

            let accountId = $(this).data('account_id');
            let deliveryOrderNo = $(this).data('delivery_order_no');

            let data = {accountId: accountId, deliveryOrderNo: deliveryOrderNo};

            $.ajax({
                method: 'post',
                url: '/xqc/customer-delivery/delivery-fba-ajax',
                data: {
                    _token: "{{ csrf_token() }}",
                    action: 'syncOutbound',
                    param: data
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            toastr.success(data.message);
                            $.pjax.reload('#pjax-container');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        });

    });
</script>
